package com.example.actionmode;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import java.util.List;


//定义一个Adapter继承与父类BaseAdapter
public class Adapter extends BaseAdapter {
    List<Item> list;  //item的list对象
    Context context; // 上下文对象


    //初始化
    public Adapter(List<Item> list, Context context) {
        this.context = context;
        this.list = list;
        //实时刷新
        notifyDataSetChanged();
    }

    //要绑定的条目数量
    @Override
    public int getCount() {
        return list.size();
    }

    //根据索引获得对象
    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    //获取条目的id
    @Override
    public long getItemId(int position) {
        return 0;
    }

    //获取条目要显示的界面
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        //如果还未加载
        if (convertView == null) {
            //加载布局文件，并将各个选项以及每个选项中的内容一一对应
            convertView = View.inflate(context, R.layout.layout, null);
            viewHolder = new ViewHolder();
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.image);
            viewHolder.textView = (TextView) convertView.findViewById(R.id.name);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //得到十六进制的颜色的int值
        int blue = Color.parseColor("#33B5E5");
        int white = Color.parseColor("#FFFFFF");
        viewHolder.textView.setText(list.get(position).getName());
        //如果被选中就改变颜色
        if (list.get(position).isSelected() == true) {
            viewHolder.textView.setBackgroundColor(blue);
            viewHolder.imageView.setBackgroundColor(blue);
        } else {
            viewHolder.textView.setBackgroundColor(white);
            viewHolder.textView.setBackgroundColor(white);
        }
        return convertView;
    }
        //创建内部类，包含imageView和textView
        class ViewHolder {
            ImageView imageView;
            TextView textView;
        }
    }
